package com.yowasa.xiuxian.engine.message.activity;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.MysComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuxianRedPacket;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.XiuxianRedPacketMapper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.ComponentUtil;
import com.yowasa.xiuxian.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@CMD(name = "/发红包")
public class RedPacketHandler extends CmdMessageHandler {
    public static final Logger logger = LoggerFactory.getLogger(RedPacketHandler.class);
    @Autowired
    private MessageHelper messageHelper;

    @Autowired
    private UserHelper userHelper;

    @Autowired
    private XiuxianRedPacketMapper xiuxianRedPacketMapper;

    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        List<String> params = getAllParam(message);//获取名称
        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 拼手气 口令 等二级指令进行后续操作\n完整指令为 /发红包 红包类型 金额 数量 ，可发拼手气红包和口令红包\n红包只能发摩拉，拼手气红包需要点击按钮，口令红包需要艾特机器人和发红包的人");
            return;
        }
        if ("拼手气".equals(params.get(0))) {
            if(params.size() < 3){
                throw new HandlerException("完整指令为 /发红包 拼手气 金额 数量");
            }
            String s = checkRedPacket(params, userBO, 1);

            MysComponent component = new MysComponent("6",1,"抢","测试");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key","抢红包");
            jsonObject.put("text",s);
            jsonObject.put("userId",userBO.getUserId());
            component.setExtra(jsonObject.toString());
            List<List<MysComponent>> componentList = ComponentUtil.genComponentList(1,component);
            messageHelper.replyMysPanel(message,userBO.getName()+"发出的红包",componentList,null,null);

        }

        if ("口令".equals(params.get(0))) {
            if(params.size() < 4){
                throw new HandlerException("完整指令为 /发红包 口令 金额 数量 口令内容 \n口令内容不允许出现逗号");
            }
            checkRedPacket(params, userBO,2);

            String s = params.get(3);
            messageHelper.replyMys(message,userBO.getName()+"发出了一个口令红包，口令为：\n"+s+"\n可使用 /抢红包 口令内容 进行抢红包");

        }
    }

    private String checkRedPacket(List<String> params,UserBO userBO,Integer type) {
        Integer gold = StrUtil.checkAndConvert2Int(params.get(1), "金额");
        Integer num = StrUtil.checkAndConvert2Int(params.get(2), "数量");
        if(userBO.getUserConfig().getGold() < gold){
            throw new HandlerException("你的摩拉不足，无法发这么多红包");
        }
        if(gold < num){
            throw new HandlerException("你这红包也太磕碜了");
        }
        userBO.getUserConfig().setGold(userBO.getUserConfig().getGold() - gold);
        userHelper.saveUserInfo(userBO);
        List<Integer> list = new ArrayList<>();
        if(num > 1){
            for (int i = 1;i<num;i++){
                list.add(fragmentation(list,gold));
            }
        }
        list.add(gold);
        list.sort(Integer::compareTo);
        XiuxianRedPacket xiuxianRedPacket = new XiuxianRedPacket();
        xiuxianRedPacket.setGold(gold);
        xiuxianRedPacket.setNum(num);
        String packetId = RandomUtil.randomString(5);
        xiuxianRedPacket.setPacketId(packetId);
        xiuxianRedPacket.setUserId(userBO.getUserId());
        xiuxianRedPacket.setType(type);
        xiuxianRedPacket.setCreateTime(new Date());
        if(type == 2){
            xiuxianRedPacket.setMsg(params.get(3));
        }
        xiuxianRedPacketMapper.insert(xiuxianRedPacket);
        for (int j = 0 ;j <list.size() ;j++){
            XiuxianRedPacket one = new XiuxianRedPacket();
            Integer getGold;
            if(j == 0){
                getGold = list.get(0);
            }else {
                getGold = list.get(j) - list.get(j-1);
            }
            one.setGold(getGold);
            one.setType(0);
            one.setPacketId(packetId);
            xiuxianRedPacketMapper.insert(one);
        }
        return packetId;

    }

    private Integer fragmentation(List<Integer> list, Integer gold) {
        int i = RandomUtil.randomInt(1, gold);
        if(list.contains(i)){
            fragmentation(list,gold);
        }
        return i;
    }


    public String desc() {
        return "发红包";
    }


}
